//calculator - A simple, customizable, lightweight date picker calendar plugin for jQuery
(function($)
{
    var defaults =
    {
        container:'',
        calId: 0,
        cssName: "default",
        showAlways: false,
        position: "absolute"
    };
    var methods =
    {
        init: function(options)
        {
            return this.each(function()
            {
                var self = $(this);
                var settings = $.extend({}, defaults);
                // Save the settings and id
                settings.calId = self[0].id+"-cal";
                if(options) {
                    settings = $.extend(settings, options);
                }
                self.data("settings", settings);
                // Bind click and focus event to show
                self
                .click(methods.show)
                .focus(methods.show);
                // If always showing, trigger click causing it to show
                if(settings.showAlways)
                {
                    setTimeout(function() {
                        self.trigger("focus");
                    }, 50);
                }
                // Bind click elsewhere to hide
                $(document).bind("click", function(e)
                {
                    methods.hide.apply(self);
                });
            });
        },
        // Show the calendar
        show: function(e)
        {
            e.stopPropagation();
            // Instead of catching blur we'll find anything that's made visible
            methods.hide.apply($("._cal").not($(this)));
            methods.update.apply($(this));
        },
        // Hide the calendar
        hide: function()
        {
            var s = $(this).data("settings");
            if($(this).length && s!=undefined)
            {
                // Hide if not showing always
                if(!s.showAlways)
                {
                    // Hide the calendar and remove class from target
                    $("#"+s.calId).hide(200);
                    $(this).removeClass("_cal");
                }
            }
        },
        // Render the calendar
        update:function()
        {
            var target = $(this);
            var settings = target.data("settings");
            // Get the calendar id
            var calId = settings.calId;
            // Build the html for the control
            var nums = '';
            for(i=0;i<=9;i++){
                nums += "<li><a href='javascript:;'>"+i+"</a></li>";
            }
            var html =
            "<div class='**'>"+
            "<ul class='**-num'>"+nums+"<li><a href='javascript:;' title='点'>.</a></li><li><a href='javascript:;' title='清空'>C</a></li><li><a href='javascript:;' title='关闭'>X</a></li>"
            "</ul>"+
            "</div>";
            // Replace css, month-year title
            html = (html.replace(/\*{2}/gi, "cal-"+settings.cssName));
            // If calendar doesn't exist, make one
            if($("#"+calId).length == 0)
            {
                if(!settings.container){
                    settings.container = $(target).parent();
                }
                $(settings.container).append
                (
                    $("<div id='"+calId+"' class='calcontainer'></div>")
                    .css(
                    {
                        "position":settings.position,
                        "z-index":settings.zIndex,
                        "left":target.outerWidth(true),
                        //"top":target.outerHeight(true)
                        "top":0
                    })
                    );
            }
            // Show calendar
            var calendar = $("#"+calId);
            calendar.html(html).show(200);
            target.addClass("_cal");
            $(".cal-"+settings.cssName+" ul li", calendar)
            .click(function(e)
            {
                e.stopPropagation();
                var num = $(this).text();
                var tv = target.val();
                var settings = target.data("settings");
                if(num == 'C'){
                    target.val('').focus();
                }else if(num == 'X'){
                    methods.hide.apply(target);
                }else{
                    if(num == '.' && target.val() == ''){
                        //第一位为点时
                        target.val("0.").focus();
                    }else if(num == '.' && target.val().indexOf('.')>0){
                        //已经存在点时
                        target.focus();
                    }else if(target.val().substr(target.val().indexOf('.')).length>=3){
                        //小数超过两位时
                        target.focus();
                    }else{
                        //正常
                        target.val(tv+num).focus();
                    }
                }
                if(settings.onChange != null && typeof settings.onChange != "undefined"){
                    settings.onChange(target, num);
                }
            });
        }
    };
    // Plugin entry
    $.fn.calculator = function(method)
    {
        if(methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        }
        else if(typeof method === "object" || !method) {
            return methods.init.apply(this, arguments);
        }
        else {
            $.error("Method "+ method + " does not exist on jQuery.calculator");
        }
    };
})(jQuery);